package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.MyFragmentPagerAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PhotoBean;
import com.sx.zongzhi.bean.PopBusinessRoadBean;
import com.sx.zongzhi.bean.PopulationDetailBean;
import com.sx.zongzhi.fragment.BasicInforEditFragment;
import com.sx.zongzhi.fragment.BusinessEditFragment;
import com.sx.zongzhi.fragment.HouseholdInforEditFragment;
import com.sx.zongzhi.presenter.PopHouseholdAddPest;
import com.sx.zongzhi.presenter.UploadFilePest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.CommentView;
import com.sx.zongzhi.view.UploadFileView;
import com.sx.zongzhi.widget.LoadDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加/编辑户籍人口
 */
public class AddPopulationActivity extends BaseActivity implements UploadFileView,CommentView{

    @BindView(R.id.tl_TabLayout)
    TabLayout tl_TabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<Fragment> fragments;
    private String orgId="";
    private List<String> businessList;
    private List<PopBusinessRoadBean> roadBeanList;
    private PopulationDetailBean detailBean;
    private  BasicInforEditFragment basicInforEditFragment;
    private  HouseholdInforEditFragment householdInforEditFragment;
    private  BusinessEditFragment businessEditFragment;
    private boolean isEdit;
    private String path;
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_household_detail);
        ButterKnife.bind(this);
        setTitleRight(R.string.submit);
        detailBean= (PopulationDetailBean) getIntent().getSerializableExtra("PopulationDetailBean");
        isEdit= getIntent().getBooleanExtra("isEdit",false);
        orgId=getIntent().getStringExtra("orgId");
        initView();
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
               submit();
                break;
        }
    }


    private void submit() {
        boolean success=basicInforEditFragment.submit();
        if (!success){
            return;
        }
        boolean success2=householdInforEditFragment.submit();
        if (!success2){
            return;
        }
        businessList=businessEditFragment.submit();
        roadBeanList=businessEditFragment.submitRoad();
        if(BaseUtil.isEmpty(detailBean.getImgUrl())|| detailBean.getImgUrl().startsWith("http")){
            submitDone();
        }else {
            List<File> files=new ArrayList<>();
            files.add(new File(detailBean.getImgUrl()));
            LoadDialog.show(this,"正在上传头像中...");
            new UploadFilePest(this).uploadFile(this,files);
        }
    }

    private void submitDone() {
        PopHouseholdAddPest addPest=new PopHouseholdAddPest(this);
        LoadDialog.show(this,"正在提交");
        if (isEdit){
            addPest.addPopHousehold("updateHouseholdStaff.action",orgId,detailBean.getId(),path,
                    businessList,roadBeanList,detailBean);
        }else {
            addPest.addPopHousehold("addHouseholdStaff.action",orgId,"",path,businessList,
                    roadBeanList,detailBean);
        }

    }


    private void initView() {
        if (isEdit){
            setTitleName("户籍人口编辑");
        }else {
            setTitleName("户籍人口新增");
        }

        List<String> titles=new ArrayList<>();
        titles.add("基本信息");
        titles.add("户籍信息");
        titles.add("业务信息");
        Bundle bundle = new Bundle();
        if (detailBean==null){
            detailBean=new PopulationDetailBean();
        }
        bundle.putSerializable("PopulationDetailBean",detailBean);
        bundle.putBoolean("isEdit",isEdit);
        bundle.putString("orgId",orgId);
        basicInforEditFragment=new BasicInforEditFragment();
        basicInforEditFragment.setArguments(bundle);
        householdInforEditFragment=new HouseholdInforEditFragment();
        householdInforEditFragment.setArguments(bundle);
        businessEditFragment=new BusinessEditFragment();
        businessEditFragment.setArguments(bundle);
        fragments=new ArrayList<>();
        fragments.add(basicInforEditFragment);
        fragments.add( householdInforEditFragment);
        fragments.add(businessEditFragment);
        tl_TabLayout.setTabMode(TabLayout.MODE_FIXED);
        //为TabLayout添加tab名称
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(0)));
        tl_TabLayout.addTab(tl_TabLayout.newTab().setText(titles.get(1)));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(2);
        //tabLayout加载viewpager
        tl_TabLayout.setupWithViewPager(viewPager);
    }


    @Override
    public void uploadSuccess(List<PhotoBean> beans) {
        if(beans!=null&&beans.size()>0){
            path=beans.get(0).getPath();
            submitDone();
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void commentSuccess(Boolean result) {
        LoadDialog.dismiss(this);
        if (result){
            ToastUtils.showToast(this,"保存成功");
            MyRxBus.get().post(new MsgEvent<>("HouseholdActivity",2,"成功"));
            finish();
        }
    }
}
